package com.hotel.ordersystem.controller;


import com.common.utils.ResultVo;
import com.hotel.ordersystem.pojo.CommentMongoDB;
import com.hotel.ordersystem.pojo.JsonVo;
import com.hotel.ordersystem.service.CommentListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;


@RestController
public class CommentController {

    @Autowired
    private CommentListService commentListService;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 从前端获取到评论的json对象，并将图片上传到七牛云
     * 将七牛云返回的路径以及评论内容封装成MongoDB需要的json对象
     * @param jsonVo
     * @return
     * @throws Exception
     */
    @PostMapping("/getComment")
    public ResultVo<CommentMongoDB> getComment(@RequestBody JsonVo jsonVo){
        System.out.println("添加评论中...");
        return commentListService.saveQiniu(jsonVo);
    }

    /**
     * 根据id获取到七牛云的数据并且渲染到前端页面
     * @param CommentId
     * @return
     */
    @GetMapping("/mongdb/getById/{commentId}")
    public CommentMongoDB MongDBGetById(@PathVariable("commentId") String CommentId){
        Query query = new Query(Criteria.where("_id").is(CommentId));
        CommentMongoDB comment = mongoTemplate.findOne(query, CommentMongoDB.class);
        return comment;
    }


}
